import axios from '@/utils/request'
export default {
  Material: {
    list: {
      export: {
        query: {
          '@display': `{
            'name' : 'entity.getName()',
            'category' : 'entity.getCategory().getName()',
            'unit' : 'entity.getUnit()'
          }`,
          limit: 10000000
        },
        label: {
          'name': '物料名',
          'category': '分类',
          'unit': '单位'
        }
      },
      list_filter: {
        'category.id': () => {
          return axios
            .get(`/manage/categories`, {
              params: {
                '@filter': 'entity.getType().getSlug() == "material"'
              }
            })
            .then(res =>
              Object.assign({ __label: '类型' },
                ...res.data.map(v => { return { [v.id]: v.name } })))
        },
        'name': '名'
      },
      list_display: [
        'name',
        'category',
        'unit'
      ]
    },
    form: {
      fields: [
        {
          property: 'category',
          field_options: { label: '类型' },
          component: {
            props: ['form', 'property'],
            data() {
              return {
                categories: []
              }
            },
            created() {
              this.axios.get(`/manage/categories`, {
                params: {
                  '@filter': 'entity.getType().getSlug() == "material"'
                }
              }).then((res) => {
                console.log(11123, res.data)
                const categories = []
                for (let i of res.data) {
                  categories.push({
                    label: `${i.name}`,
                    value: i.id
                  })
                }
                this.categories = categories
                console.log(categories)
              })
            },
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} filterable placeholder='类型'>{
                  this.categories.map(type => {
                    return <el-option key={type.value} label={type.label} value={type.value} />
                  })
                }</el-select>
              )
            }
          }
        },
        'name',
        'unit'
      ]
    }
  }
}

